<?php

class Default_Model_Items extends Zend_Db_Table_Abstract
{
    protected $_name = 'items';
    public function getDataByEquipmentId($equipmentId, $field)
    {
		$select = $this->select();
   		$select->from(array('i'=>'items'), $field)
   				->where('equipmentId = ?', $equipmentId);

   		return $this->fetchAll($select);
    }
    public function getDataByItemId($itemId)
    {
		$select = $this->select();
   		$select->from(array('i'=>'items'))
   				->where('itemId = ?', $itemId);

   		return $this->fetchRow($select)->toArray();
    }
    public function checkMaxCode($code){
        $query = $this->select();
        $query->from(array('i' => $this->_name), array('code'))
                ->where('code like "'.$code.'%"')
                ->order('code DESC')
                ->limit('0,1');
                //echo $query;exit;
        return $this->fetchRow($query);
    }
    public function getItems($equipmentId)
    {
		$select = $this->select();
   		$select->from(array('i'=>'items'))
   				->where('equipmentId = ?', $equipmentId);
   		return $this->fetchAll($select);
    }

    public function getItemspa($equipmentId, $page, $row)
    {
		$select = $this->select();
   		$select->from(array('i'=>'items'))
   				->where('equipmentId = ?', $equipmentId)
                ->limitPage($page, $row);
   		return $this->fetchAll($select);
    }
    public function getCountItems($equipmentId)
    {
		$select = $this->select();
   		$select->from(array('i'=>'items'), 'count(i.itemId) as Total')
   				->where('equipmentId = ?', $equipmentId);
        return $this->fetchRow($select);
    }
    public function getItemsNotify($equipmentId)
    {
        $dataNotify = mktime(0, 0, 0, date("m")+1, date("d"), date("Y"));
        $dataNotify = date("Y-m-d", $dataNotify);
		$select = $this->select();
   		$select->from(array('i'=>'items'))
   				->where('equipmentId = ?', $equipmentId)
                ->where('i.duration <= ?',$dataNotify)
                ->where('i.duration > i.startDate')
                ->where('i.duration > 0')
                ->where('i.expire != ?',1);

   		return $this->fetchAll($select);
    }
	public function getOwner()
	{
		$select = $this->select()
                        ->from($this->_name, array('owner'))
                        ->where('owner != ?','')
                        ->group('owner')
                        ->order('owner ASC');
		return $this->fetchAll($select);
	}
    public function deleteItemByEquipment ($equipmentId)
    {
    	$this->delete( 'equipmentId =' . (int) $equipmentId );
    }
     public function deleteItemByItemId ($itemId)
    {
    	$this->delete( 'itemId =' . (int) $itemId );
    }
    public function updateEquipmentidItem($itemId, $equipmentId=0)
    {
        $data=array('equipmentId'=>$equipmentId);
        return $this->update($data, 'itemId = '.(int) $itemId);
        
    }
    public function updateItem($itemId, $itemName, $serialNumber, $rentItem, $rentEnd, $priceItem, $equipmentId, $startDate, $duration,
    $holders, $priceNow,$history, $info, $addendums, $owner, $expire, $createUser, $createDate)
    {
        $data = array(
            'itemName' => $itemName,'serialNumber' => $serialNumber,'rentItem' => $rentItem,
            'rentEnd' => $rentEnd,'priceItem' => $priceItem,'equipmentId' => $equipmentId, 
            'startDate' => $startDate,'duration' => $duration,'holders' => $holders,
            'priceNow' => $priceNow,'history' => $history,'info' => $info, 
            'addendums' => $priceNow,'owner' => $owner,'expire' => $expire,'createUser' => $createUser,'createDate' => $createDate,  
            );
        $this->update($data, 'itemId = ' . (int)$itemId);
    }
    public function addItem ($itemName,$code, $category, $serialNumber, $rentItem, $rentEnd, $priceItem, $equipmentId, $startDate, $duration,
    $holders, $priceNow,$history, $info, $addendums, $owner, $expire, $createUser, $createDate)
    {       
        try{
        	$data = array(
            'itemName' => $itemName,'serialNumber' => $serialNumber,'code' => $code,'category' => $category, 'rentItem' => $rentItem,
            'rentEnd' => $rentEnd,'priceItem' => $priceItem,'equipmentId' => $equipmentId, 
            'startDate' => $startDate,'duration' => $duration,'holders' => $holders,
            'priceNow' => $priceNow,'history' => $history,'info' => $info, 
            'addendums' => $priceNow,'owner' => $owner,'expire' => $expire,'createUser' => $createUser,'createDate' => $createDate,  
            );
            $idAdd = $this->insert($data);
            return $idAdd;
        }
        catch(Zend_Exception $exp)
        {
        	
        }
    }
    
   

	

}

























